﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using OpenExpressApp.Command;
using OpenExpressApp.MetaAttribute;
using System.ComponentModel.Composition;
using OpenExpressApp.Library;

namespace OpenExpressApp.Module
{
    /// <summary>
    /// 加载Library
    /// </summary>
    [Export(typeof(IModule))]
    public class ExpressAppModule : LoadMetaDataModule
    {
        /// <summary>
        /// 加入Library目录下的业务模型 命令等
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();

            //查找Library目录下的所有程序集
            string exePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string libraryPath = Path.Combine(exePath, "Library");
            Directory.CreateDirectory(libraryPath);
            var files = Directory.GetFiles(libraryPath, "*.dll").ToList();
            files.Insert(0, Path.Combine(exePath, "OpenExpressApp.Library.dll"));

            foreach (string fileName in files)
            {
                var assembly = Assembly.LoadFrom(fileName);
                foreach (var type in this.GetEntityTypes(assembly))
                {
                    //加入业务模型 命令等
                    BusinessObjectInfo bOInfo = ApplicationModel.AddBusinessObject(type);
                    bOInfo.SetDefaultCommands();
                }
            }
        }
    }
}
